
package com.rt.schedulenew.utils.util;
import java.io.ByteArrayInputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import org.springframework.stereotype.Component;
@Component
public class MarshallerUtils
{
    private static String xmlCoding;
    
    public Object toBeanFromFile(String absPath, String fileName, Class type) throws Exception {
        String filePath = absPath + fileName;
        InputStream ins = null;
        try {
            ins = new FileInputStream(new File(filePath));
        }
        catch (Exception e) {
            throw new Exception("读{" + filePath + "}文件失败！", e);
        }
        Object obj = null;
        try {
            Unmarshaller unMar = this.createUnMarshallerByType(type);
            obj = unMar.unmarshal(ins);
        }
        catch (Exception e2) {
            throw new Exception("解析{" + filePath + "}文件失败！", e2);
        }
        if (ins != null) {
            ins.close();
        }
        return obj;
    }
    
    private Marshaller createMarshallerByType(Class type) {
        Marshaller mar = null;
        try {
            JAXBContext jax = JAXBContext.newInstance(new Class[] { type });
            mar = jax.createMarshaller();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return mar;
    }
    
    private Unmarshaller createUnMarshallerByType(Class type) {
        Unmarshaller unMar = null;
        try {
            JAXBContext jax = JAXBContext.newInstance(new Class[] { type });
            unMar = jax.createUnmarshaller();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return unMar;
    }
    
    public String parseObjectXmlData(Object obj) {
        String backXml = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Marshaller mar = this.createMarshallerByType(obj.getClass());
            mar.marshal(obj, (OutputStream)baos);
            backXml = baos.toString();
            backXml = new String(baos.toByteArray(), MarshallerUtils.xmlCoding);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return backXml;
    }
    
    public String parseObjectXmlData(Object obj, String enCoding) {
        String backXml = null;
        try {
            if (enCoding != null) {
                MarshallerUtils.xmlCoding = enCoding;
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Marshaller mar = this.createMarshallerByType(obj.getClass());
            mar.setProperty("jaxb.encoding", (Object)MarshallerUtils.xmlCoding);
            mar.marshal(obj, (OutputStream)baos);
            backXml = baos.toString();
            backXml = new String(baos.toByteArray(), MarshallerUtils.xmlCoding);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return backXml;
    }
    
    public Object parseXmlDataObject(Class type, String xmlData) {
        if (xmlData == null || xmlData.trim().length() == 0) {
            return null;
        }
        try {
            Unmarshaller unMar = this.createUnMarshallerByType(type);
            ByteArrayInputStream bais = new ByteArrayInputStream(xmlData.getBytes(MarshallerUtils.xmlCoding));
            return unMar.unmarshal((InputStream)bais);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public Object parseXmlDataObject(Class type, String xmlData, String enCoding) {
        if (xmlData == null || xmlData.trim().length() == 0) {
            return null;
        }
        try {
            MarshallerUtils.xmlCoding = enCoding;
            Unmarshaller unMar = this.createUnMarshallerByType(type);
            ByteArrayInputStream bais = new ByteArrayInputStream(xmlData.getBytes(MarshallerUtils.xmlCoding));
            return unMar.unmarshal(bais);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    static {
        MarshallerUtils.xmlCoding = "UTF-8";
    }
}
